package com.itstudio.portal.controller;

import com.itstudio.portal.entity.RestResponse;
import com.itstudio.portal.entity.dto.AuthInfoDto;
import com.itstudio.portal.entity.dto.AuthQueryDto;
import com.itstudio.portal.entity.dto.CorporateMaintenanceDto;
import com.itstudio.portal.service.AuthInfoService;
import com.itstudio.portal.service.DictRegionService;
import com.itstudio.portal.service.IndustryService;
import com.itstudio.portal.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Description:
 * @Author: zhaopengbo
 * @Date: 2024/11/13
 */
@RestController
@RequestMapping("/api/auth")
@Slf4j
public class AuthController {
    @Resource
    private AuthInfoService authInfoService;

    @Resource
    private JwtUtil jwtUtil;

    //认证信息
    @PostMapping
    public RestResponse auth(@RequestBody AuthInfoDto authInfoDto, HttpServletRequest request) {
        String authorization = request.getHeader("Authorization");
        String token = authorization.substring(7);
        String currentId = jwtUtil.verifyToken(token);
        authInfoDto.setUserId(Long.valueOf(currentId));

        log.info("currentId====>"+currentId);
        return authInfoService.submitAuthInfo(authInfoDto);
    }

    //更改
    @PutMapping
    public RestResponse updateAuthInfo(@RequestBody AuthInfoDto authInfoDto, HttpServletRequest request) {
        log.warn("authInfoDto: {}", authInfoDto.getId());
        if(authInfoDto.getUserId() == null){
            String authorization = request.getHeader("Authorization");
            String token = authorization.substring(7);
            String currentId = jwtUtil.verifyToken(token);
            authInfoDto.setUserId(Long.valueOf(currentId));
        }
        return authInfoService.updateAuthInfo(authInfoDto);
    }

    //企业维护
    @PutMapping("/maintenance")
    public RestResponse corporateMaintenance(@RequestBody CorporateMaintenanceDto corporateMaintenanceDto) {
        return authInfoService.updateMaintenance(corporateMaintenanceDto);

    }

    @GetMapping("/maintenance")
    public RestResponse getCorporateMaintenance(HttpServletRequest request) {
        String authorization = request.getHeader("Authorization");
        String token = authorization.substring(7);
        String currentId = jwtUtil.verifyToken(token);

        return authInfoService.getMaintenance(Long.valueOf(currentId));
    }

    //查看
    @GetMapping
    public RestResponse getAuthInfoList(HttpServletRequest request,
                                        @RequestParam(required = false) Long userId,
                                        @RequestParam(required = false) Long id) {
        if (userId == null) {
            String authorization = request.getHeader("Authorization");

            String token = authorization.substring(7);
             userId = Long.valueOf(jwtUtil.verifyToken(token));
        }

        return authInfoService.selectAuthInfo(userId, id);
    }

   /* @GetMapping("/list")
    public RestResponse getAuthInfoList(AuthQueryDto authQueryDto) {
        log.warn("authQueryDto:{}", authQueryDto);
        return authInfoService.selectAllAuthInfo(authQueryDto);
    }*/

    //删除
    @DeleteMapping("/{authId}")
    public RestResponse deleteAuthInfo(@PathVariable Long authId) {
        return authInfoService.deleteAuthInfo(authId);
    }


}
